package com.strongbj.iot.devices.newlight.request.listener;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.strongbj.core.message.MessageHandleContext;
import com.strongbj.iot.devices.newlight.message.NewLightMQMessage;
import com.strongbj.iot.devices.newlight.request.handle.ActiveTagHandle;
import com.strongbj.iot.devices.newlight.request.handle.BindTagHandle;
import com.strongbj.iot.devices.newlight.request.handle.CloseInventoryHandle;
import com.strongbj.iot.devices.newlight.request.handle.CloseTagHandle;
import com.strongbj.iot.devices.newlight.request.handle.GetTagsReaderRelationHandle;
import com.strongbj.iot.devices.newlight.request.handle.InventoryHandle;
import com.strongbj.iot.devices.newlight.request.handle.OpenInventoryHandle;
import com.strongbj.iot.devices.newlight.request.handle.ReaderAwakenHandle;
import com.strongbj.iot.devices.newlight.request.handle.UnBindTagHandle;

@Component
public class MQMessageOfNewLightListener extends MessageListenerAdapter {

	private static Logger logger = LogManager.getLogger(MQMessageOfNewLightListener.class.getName());

	private MessageHandleContext<NewLightMQMessage, Object> messageHandleContent;

	public MQMessageOfNewLightListener() {
		super();
		this.messageHandleContent = new MessageHandleContext<>();
		// 添加消息处理类 ----（盘点请求）
		this.messageHandleContent.addHandleClass(new InventoryHandle());
		// 标签绑定请求
		this.messageHandleContent.addHandleClass(new BindTagHandle());
		this.messageHandleContent.addHandleClass(new UnBindTagHandle());
		this.messageHandleContent.addHandleClass(new CloseInventoryHandle());
		this.messageHandleContent.addHandleClass(new OpenInventoryHandle());
		this.messageHandleContent.addHandleClass(new ActiveTagHandle());
		this.messageHandleContent.addHandleClass(new GetTagsReaderRelationHandle());
		this.messageHandleContent.addHandleClass(new CloseTagHandle());
		this.messageHandleContent.addHandleClass(new ReaderAwakenHandle());
	}
	
//	@JmsListener(destination = "wh.reader.*.*.*.message", containerFactory = "handleJmsListener")
//	public void onMessage(Message message, Session session) throws JMSException {
//		if (message instanceof TextMessage) {
//			TextMessage tm = (TextMessage) message;
//			String json = tm.getText();
//			logger.info("收到MQ消息" + json);
//		} else {
//			logger.info("无法解析的mq对象消息:" + message.getClass().getName());
//		}
//	}
	
	@JmsListener(destination = "wh.reader.command.request", containerFactory = "handleJmsListener")
	public void onMessage(Message message, Session session) throws JMSException {
		if (message instanceof TextMessage) {
			TextMessage tm = (TextMessage) message;
			String json = tm.getText();
			logger.info("收到MQ消息" + json);
			NewLightMQMessage rm = JSON.parseObject(json, new TypeToken<NewLightMQMessage>() {
			}.getType());
			this.messageHandleContent.handle(null, rm);
		} else {
			logger.info("无法解析的mq对象消息:" + message.getClass().getName());
		}
	}

	@Bean
	public JmsListenerContainerFactory<?> handleJmsListener(ConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setPubSubDomain(true);
		factory.setConnectionFactory(activeMQConnectionFactory);
		// 设置连接数
		factory.setConcurrency("1");
		// 重连间隔时间
		factory.setRecoveryInterval(1000L);
		return factory;
	}
}
